package cn.zd.demo1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.zd.demo1.R;
import cn.zd.demo1.data.Fruits;

public class MyGridViewBaseAdapter extends BaseAdapter {
    private List<Fruits> dataList = Fruits.initData;

    private Context context;

    public MyGridViewBaseAdapter(Context context) {
        this.context = context;
    }


    public Fruits getData(int position) {
        return dataList.get(position);
    }
    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.spinner_item,null);
            LinearLayout ly_list_view = convertView.findViewById(R.id.ly_list_view);
            ImageView iv = convertView.findViewById(R.id.iv_img);
            TextView tv_name = convertView.findViewById(R.id.tv_name);
            TextView tv_detail = convertView.findViewById(R.id.tv_detail);
            holder = new MyViewHolder(iv,tv_name,tv_detail,null,ly_list_view);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.getIv().setImageResource(dataList.get(position).getImg());
        holder.getTv_name().setText(dataList.get(position).getName());
        holder.getTv_detail().setText(dataList.get(position).getDetail());
        /*if(position % 2 == 1) {
            holder.getBtn_1().setVisibility(View.VISIBLE);
            holder.getBtn_1().setOnClickListener(v -> {
                Toast.makeText(context,"通过按钮点选了"+dataList.get(position).getName(),Toast.LENGTH_SHORT).show();
            });
        }else {
            holder.getBtn_1().setVisibility(View.INVISIBLE);
            holder.getBtn_1().setOnClickListener(null);
        }*/
        //通过代码阻止layout事件向下传递
        holder.getLy_list_view().setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        return convertView;
    }
}
